/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.mnt.service.impl

import cn.hutool.core.util.IdUtil
import me.zhengjie.common.utils.PageResult
import me.zhengjie.common.utils.PageUtil
import me.zhengjie.common.utils.QueryHelp
import me.zhengjie.common.utils.ValidationUtil
import jakarta.persistence.criteria.CriteriaBuilder
import jakarta.persistence.criteria.CriteriaQuery
import jakarta.persistence.criteria.Root
import me.zhengjie.modules.mnt.repository.DeployHistoryRepository
import me.zhengjie.modules.mnt.service.DeployHistoryService
import me.zhengjie.modules.mnt.service.dto.DeployHistoryQueryCriteria
import me.zhengjie.modules.mnt.service.mapstruct.DeployHistoryMapper
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.domain.Specification
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.IOException
import java.util.function.Supplier
import jakarta.servlet.http.HttpServletResponse
import me.zhengjie.common.utils.FileUtil
import me.zhengjie.modules.mnt.domain.Database
import me.zhengjie.modules.mnt.domain.DeployHistory
import me.zhengjie.modules.mnt.service.dto.DatabaseDto
import me.zhengjie.modules.mnt.service.dto.DeployHistoryDto

/**
 * @author zhanghouying
 * @date 2019-08-24
 */
@Service
class DeployHistoryServiceImpl (
    private val deployhistoryRepository: DeployHistoryRepository? = null,
    private val deployhistoryMapper: DeployHistoryMapper? = null
): DeployHistoryService {
    override fun queryAll(criteria: DeployHistoryQueryCriteria?, pageable: Pageable?): PageResult<DeployHistoryDto?> {
        val page: Page<DeployHistory> =
            deployhistoryRepository!!.findAll(Specification<DeployHistory?> { root: Root<DeployHistory?>?, criteriaQuery: CriteriaQuery<*>?, criteriaBuilder: CriteriaBuilder? ->
                QueryHelp.getPredicate(
                    root!!,
                    criteria,
                    criteriaBuilder!!
                )
            }, pageable!!) as Page<DeployHistory>

        return PageUtil.toPage(page.map{ deployhistoryMapper!!.toDto(it!!)}) as PageResult<DeployHistoryDto?>
    }

    override fun queryAll(criteria: DeployHistoryQueryCriteria?): List<DeployHistoryDto?> {
        return deployhistoryMapper!!.toDto(deployhistoryRepository!!.findAll(Specification<DeployHistory?> { root: Root<DeployHistory?>?, criteriaQuery: CriteriaQuery<*>?, criteriaBuilder: CriteriaBuilder? ->
            QueryHelp.getPredicate(
                root!!,
                criteria,
                criteriaBuilder!!
            )
        }))!!
    }

    override fun findById(id: String?): DeployHistoryDto {
        val deployhistory: DeployHistory = deployhistoryRepository!!.findById(id!!).orElseGet(Supplier { DeployHistory() })!!
        ValidationUtil.isNull(deployhistory.id, "DeployHistory", "id", id)
        return deployhistoryMapper!!.toDto(deployhistory)!!
    }

    @Transactional(rollbackFor = [Exception::class])
    override fun create(resources: DeployHistory) {
        resources.id= IdUtil.simpleUUID()
        deployhistoryRepository!!.save<DeployHistory>(resources)
    }

    @Transactional(rollbackFor = [Exception::class])
    override fun delete(ids: Set<String?>) {
        for (id in ids) {
            deployhistoryRepository!!.deleteById(id!!)
        }
    }

    @Throws(IOException::class)
    override fun download(queryAll: List<DeployHistoryDto?>, response: HttpServletResponse?) {
        val list: MutableList<Map<String?, Any?>> = ArrayList()
        for (deployHistoryDto in queryAll) {
            val map: MutableMap<String?, Any?> = LinkedHashMap()
            map["部署编号"] = deployHistoryDto!!.deployId!!
            map["应用名称"] = deployHistoryDto.appName!!
            map["部署IP"] = deployHistoryDto.ip!!
            map["部署时间"] = deployHistoryDto.deployDate!!
            map["部署人员"] = deployHistoryDto.deployUser!!
            list.add(map)
        }
        FileUtil.downloadExcel(list, response!!)
    }
}